import * as actionTypes from './constants'
import {
    getHotKeywordsRequest,
    getSuggestListRequest,
    getResultSongsListRequest
} from '@/api/request'

// 供 异步请求 action 用，不 export
const changeHotKeywords = (data) => ({
    type: actionTypes.SET_HOT_KEYWOEDS,
    data
})
const changeSuggestList = (data) => ({
    type: actionTypes.SET_SUGGEST_LIST,
    data
})
const changeResultSongs = (data) => ({
    type: actionTypes.SET_SONGS_LIST,
    data
})
export const changeEnterLoading = (data) => ({
    type: actionTypes.SET_ENTER_LOADING,
    data
})
export const getHotKeywords = () => {
    return (dispatch) => {
        getHotKeywordsRequest()
            .then(data => {
                let list = data.result.hots;
                // const action = changeHotKeywords(list)
                dispatch(changeHotKeywords(list))
            })
    }
}
export const getSuggestList = (query) => {
    return dispatch => {
        getSuggestListRequest(query)
            .then(data => {
                if (!data) return;
                let res = data.result || [];
                dispatch(changeSuggestList(res))
                // dispatch(changeEnterLoading(false))
            })
        getResultSongsListRequest(query)
            .then(data => {
                if (!data) return;
                let res = data.result.songs || [];
                dispatch(changeResultSongs(res))
                dispatch(changeEnterLoading(false))
            })
    }
}